<template>
  <div class="rc-cont">
    <!-- <flexbox v-if="!isSeas"
             class="rc-head"
             direction="row-reverse">
      <el-button class="rc-head-item"
                 @click.native="handleClick('remove')"
                 type="primary">删除</el-button>
      <el-button class="rc-head-item"
                 @click.native="handleClick('edit')"
                 type="primary">编辑</el-button>
      <el-button class="rc-head-item"
                 @click.native="handleClick('add')"
                 type="primary">新增满意度</el-button>
    </flexbox> -->
    <el-table :data="list"
              :height="tableHeight"
              stripe
              style="width: 100%;border: 1px solid #E6E6E6;"
              :header-cell-style="headerRowStyle"
              :cell-style="cellStyle"
              @row-click="handleRowClick"
              @selection-change="handleSelectionChange">
      <!-- <el-table-column show-overflow-tooltip
                       type="selection"
                       align="center"
                       width="55"
                       :selectable="handleSelectable">
      </el-table-column> -->
      <el-table-column v-for="(item, index) in fieldList"
                       :key="index"
                       show-overflow-tooltip
                       :width="item.width"
                       :align="item.align"
                       :prop="item.prop"
                       :label="item.label">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="90"
          v-if="editFeedbackAuth"
      >
          <template slot-scope="scope">
              <el-button
                  @click="handleClick('edit', scope)"
                  type="text"
                  size="small"
              >编辑</el-button>
              <!-- <el-button
                  @click="handleClick('delete', scope)"
                  type="text"
                  size="small"
              >删除</el-button> -->
          </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="editTitle"
               v-loading="loading"
               :visible.sync="editPermissionShow"
               :append-to-body="true"
               width="400px">
      <div class="handle-box">
        <flexbox class="handle-item">
          <!-- <div class="handle-item-name">权限：</div> -->
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6}"
            placeholder="请输入您的评价"
            v-model="editInput">
          </el-input>
        </flexbox>
      </div>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click.native="editPermissionShow=false">取消</el-button>
        <el-button type="primary"
                   @click.native="handleEditConfirm">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script type="text/javascript">
import { crmFeedbackListAPI, crmDelFeedbackAPI, crmEditFeedbackAPI  } from '@/api/customermanagement/common'
import loading from '../mixins/loading'
import { mapGetters } from "vuex";

export default {
  name: 'relative-team', //相关团队  可能再很多地方展示 放到客户管理目录下
  components: {
  },
  computed: {},
  mixins: [loading],
  data() {
    return {
      list: [],
      fieldList: [],
      tableHeight: '400px',
      teamsDialogShow: false, //是否展示添加团队成员窗口
      handleType: 1, // 权限类型
      editPermissionShow: false, //编辑权限接口展示
      selectionList: [], //勾选的数据
      infoPermissionShow: false, // 详情
      editTitle: "满意度",
      editId: "", // 编辑id
      editInput: "", // 编辑新增
    }
  },
  watch: {
    id: function(val) {
      this.list = []
      this.getDetail()
    }
  },
  props: {
    /** 模块ID */
    id: [String, Number],
    /** 联系人人下 新建商机 需要联系人里的客户信息  合同下需要客户和商机信息 */
    detail: {
      type: Object,
      default: () => {
        return {}
      }
    },
    /** 没有值就是全部类型 有值就是当个类型 */
    crmType: {
      type: String,
      default: ''
    },
    /** 是公海 默认是客户 */
    isSeas: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    ...mapGetters(["crm"]),
    // 员工创建权限
    editFeedbackAuth() {
        return this.crm && this.crm.contract && this.crm.contract.editfeedback;
    },
  },
  mounted() {
    this.fieldList.push({ prop: 'realname', width: '100', label: '姓名' })
    this.fieldList.push({ prop: 'structure_name', width: '150', label: '职位' })
    this.fieldList.push({ prop: 'group_name', width: '150', label: '团队角色' })
    // this.fieldList.push({ prop: 'authority', width: '200', label: '权限' })
    this.fieldList.push({ prop: 'feedback_content', label: '满意度', align: 'left' })
    this.getDetail()
  },
  activated: function() {},
  deactivated: function() {},
  methods: {
    getDetail() {
      this.loading = true
      crmFeedbackListAPI({
        // types: 'crm_' + this.crmType,
        contract_id: this.id
      })
        .then(res => {
          this.loading = false
          this.list = res.data
        })
        .catch(() => {
          this.loading = false
        })
    },
    //当选择项发生变化时会触发该事件
    handleSelectionChange(val) {
      this.selectionList = val
    },
    handleClick(type, scope) {
      if (type == 'add') {
        this.editTitle = "新增满意度"
        this.editInput = ""
        this.editPermissionShow = true
      } else  if (type == 'edit') {
        this.editTitle = "编辑满意度"
        this.editId = scope.row.id
        this.editInput = scope.row.feedback_content ? scope.row.feedback_content : ""
        this.editPermissionShow = true
      } else if (type == 'record') {
        this.infoPermissionShow = true
        this.$alert(scope.row.feedback_content, '满意度详情', {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
      } else if (type == 'delete') {
        this.$confirm('此操作将删除该条记录，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            var params = {
              contract_id: this.detail[this.crmType + '_id'],
              user_id: scope.row.id
            }
            this.loading = true
            crmDelFeedbackAPI(params)
              .then(res => {
                this.$message({
                  type: 'success',
                  message: res.data
                })
                this.loading = false
                this.getDetail()
              })
              .catch(() => {
                this.loading = false
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      }
    },
    /** 添加操作 */
    handleCallBack(data) {
      this.getDetail()
    },
    /** 新增/编辑操作确定 */
    handleEditConfirm() {
      var params = {}
      if (this.editId) {
        params.contract_id = this.detail[this.crmType + '_id']
        params.user_id = this.editId
      } else {
        params.contract_id = this.detail[this.crmType + '_id']
      }
      params.content = this.editInput
      this.loading = true
      crmEditFeedbackAPI(params)
        .then(res => {
          this.editPermissionShow = false
          this.$message({
            type: 'success',
            message: res.data
          })
          this.loading = false
          this.getDetail()
        })
        .catch(() => {
          this.loading = false
        })
    },
    //返回值用来决定这一行的 CheckBox 是否可以勾选
    handleSelectable(row, index) {
      /** row 是数据源 正式调试以他为主  index 是行 */
      if (row.type == 0) {
        // 0负责人，1只读，2读写
        return false
      }
      return true
    },
    //当某一行被点击时会触发该事件
    handleRowClick(row, column, event) {},
    /** 通过回调控制表头style */
    headerRowStyle({ row, column, rowIndex, columnIndex }) {
      return { textAlign: 'center' }
    },
    /** 通过回调控制style */
    cellStyle({ row, column, rowIndex, columnIndex }) {
      return { textAlign: 'center' }
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../styles/relativecrm.scss';
</style>
